package com.polaris.live.dialog

import android.content.Context
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.activity.BaseVmActivity
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.databinding.DialogManageMuteBinding
import com.polaris.live.databinding.DialogToolCardSureBinding
import com.polaris.live.net.userService
import com.polaris.live.resp.back_resp.ResourceOptionBaseVo
import com.polaris.live.resp.back_resp.UserBackpackConsumableResp
import com.polaris.live.resp.back_resp.UserConsumableDailyRewardResp
import com.polaris.live.resp.back_resp.UserMuteOptionsResp
import com.polaris.live.resp.go_resp.ManagerMuteRequest
import com.polaris.live.resp.go_resp.ManagerUnMuteRequest
import com.polaris.live.translation.R.*
import com.polaris.live.utils.image.ImageUtils

/**
 * ManageMuteDialog 管理禁言时间
 *
 * @author Created by 天晴 on 2025/1/15/016 19:40
 */
class ManageMuteDialog(
    context: Context,
    private val mViewModel: BaseViewModel,
    private val otherId: Long,
    private val item: UserMuteOptionsResp,
    private val muteBack: (mute: Boolean) -> Unit,
) : CenterPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_manage_mute
    }

    private lateinit var mBinding: DialogManageMuteBinding
    private var nowType: ResourceOptionBaseVo? = null

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogManageMuteBinding.bind(popupImplView)
        item.muteDuration.getOrNull(0)?.let {
            setData(it)
        }
        mBinding.selectCl.setOnSingleClickListener {
            ManageSelectDialog.show(context, mBinding.selectCl, item.muteDuration) {
                setData(it)
            }
        }
        mBinding.tvSure.setOnSingleClickListener {
            managerMuteRequest()
        }
        mBinding.tvCancel.setOnSingleClickListener {
            dismiss()
        }
    }

    private fun setData(data: ResourceOptionBaseVo) {
        nowType = data
        mBinding.timeTv.text = data.name

    }

    private fun managerMuteRequest() {
        nowType?.let {
            val request = ManagerMuteRequest(it.type, otherId)
            mViewModel.requestLoading({ userService.managerMute(request) }, {
                ToastGravity.showGravity(StringUtils.getString(string.forbidden_successfully))
                muteBack.invoke(true)
                dismiss()
            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }

    }


    companion object {

        fun show(context: Context, mViewModel: BaseViewModel, otherId: Long, muteBack: (mute: Boolean) -> Unit) {
            mViewModel.requestLoading({ userService.managerMuteOptions() }, {
                XPopup.Builder(context)
                    .isViewMode(true)
                    .moveUpToKeyboard(false)
                    .isDestroyOnDismiss(true)
                    .asCustom(ManageMuteDialog(context, mViewModel, otherId, it, muteBack))
                    .show()
            })

        }
    }
}